package com.ideaaedi.zoo.example.springboot.auth.satoken.config;

import cn.dev33.satoken.dao.SaTokenDaoForRedisTemplate;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * sa-token相关数据，使用统一前缀存储进redis
 * <pre>
 *  <h3>具体实现步骤：</h3>
 *  <ul>
 *      <li>
 *          第一步：排除{@link SaTokenDaoForRedisTemplate}自动装配。示例两种通过配置文件排除的方式：
 *          <br />方式一：<br />
 *          {@code
 *           spring:
 *             autoconfigure:
 *               exclude:
 *                 - cn.dev33.satoken.dao.SaTokenDaoForRedisTemplate
 *          }
 *
 *          <br />方式二：<br />
 *
 *          {@code
 *           zoo:
 *             foundation:
 *               exclude-autoconfigures:
 *                 cn.dev33.satoken.dao.SaTokenDaoForRedisTemplate: true
 *          }
 *      </li>
 *      <li>
 *          第二步：重写SaTokenDaoForRedisTemplate并注册进spring，参考这里的{@link RedisPrefixSaTokenDaoForRedisTemplate}
 *      </li>
 *  </ul>
 * </pre>
 *
 * @author <font size = "20" color = "#3CAA3C"><a href="https://gitee.com/JustryDeng">JustryDeng</a></font> <img
 * src="https://gitee.com/JustryDeng/shared-files/raw/master/JustryDeng/avatar.jpg" />
 * @since 1.0.0
 */
@Component
public class RedisPrefixSaTokenDaoForRedisTemplate extends SaTokenDaoForRedisTemplate {
    
    private static final String REDIS_CACHE_PREFIX = "zoo-demo:";
    
    /**
     * 定制{@link StringRedisTemplate}、{@link RedisConnectionFactory}
     */
    protected void initMore(RedisConnectionFactory connectionFactory) {
        /// super.stringRedisTemplate.setStringSerializer();
    }
    
    @Override
    public String get(String key) {
        key = appendCachePrefix(key);
        return super.get(key);
    }
    
    @Override
    public void set(String key, String value, long timeout) {
        key = appendCachePrefix(key);
        super.set(key, value, timeout);
    }
    
    @Override
    public void update(String key, String value) {
        key = appendCachePrefix(key);
        super.update(key, value);
    }
    
    @Override
    public void delete(String key) {
        key = appendCachePrefix(key);
        super.delete(key);
    }
    
    @Override
    public long getTimeout(String key) {
        key = appendCachePrefix(key);
        return super.getTimeout(key);
    }
    
    @Override
    public void updateTimeout(String key, long timeout) {
        key = appendCachePrefix(key);
        super.updateTimeout(key, timeout);
    }
    
    @Override
    public Object getObject(String key) {
        key = appendCachePrefix(key);
        return super.getObject(key);
    }
    
    @Override
    public void setObject(String key, Object object, long timeout) {
        key = appendCachePrefix(key);
        super.setObject(key, object, timeout);
    }
    
    @Override
    public void updateObject(String key, Object object) {
        key = appendCachePrefix(key);
        super.updateObject(key, object);
    }
    
    @Override
    public void deleteObject(String key) {
        key = appendCachePrefix(key);
        super.deleteObject(key);
    }
    
    @Override
    public long getObjectTimeout(String key) {
        key = appendCachePrefix(key);
        return super.getObjectTimeout(key);
    }
    
    @Override
    public void updateObjectTimeout(String key, long timeout) {
        key = appendCachePrefix(key);
        super.updateObjectTimeout(key, timeout);
    }
    
    @Override
    public List<String> searchData(String prefix, String keyword, int start, int size, boolean sortType) {
        prefix = appendCachePrefix(prefix);
        return super.searchData(prefix, keyword, start, size, sortType);
    }
    
    /**
     * 增加sa-token相关redis cache前缀
     */
    private static String appendCachePrefix(String key) {
        if (key == null) {
            return null;
        }
        if (key.startsWith(REDIS_CACHE_PREFIX)) {
            return key;
        }
        return REDIS_CACHE_PREFIX + key;
    }
}
